package com.lyfei.ydc.api;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyfei.ydc.core.LocalUser;
import com.lyfei.ydc.core.interceptors.ScopeLevel;
import com.lyfei.ydc.domain.entity.Reservation;
import com.lyfei.ydc.exception.http.ParameterException;
import com.lyfei.ydc.service.ReservationService;
import org.apache.commons.lang3.RegExUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/reservation")
public class ReservationController {
    @Autowired
    private ReservationService reservationService;

    @ScopeLevel()
    @PostMapping("/saveOrUpdate")
    public Reservation saveOrUpdate(@RequestBody Reservation reservation) {
        if(StrUtil.isEmpty(reservation.getUserName()) || StrUtil.isEmpty(reservation.getPhone()) || StrUtil.isEmpty(reservation.getAddress())){
            throw new ParameterException(40013);
        }
        if(reservation.getReservationTime() == null) {
            throw new ParameterException(40014);
        }

        if(!Validator.isMobile(reservation.getPhone())){
            throw new ParameterException(40015);
        }

        reservation.setUserId(LocalUser.getUser().getId());
        this.reservationService.saveOrUpdate(reservation);

        return reservation;
    }

    @ScopeLevel()
    @GetMapping("/list")
    public List<Reservation> getReservationList() {

        return this.reservationService.list(new LambdaQueryWrapper<Reservation>()
                .eq(Reservation::getUserId, LocalUser.getUser().getId())
                .gt(Reservation::getReservationTime, DateUtil.beginOfDay(new Date()))
                .eq(Reservation::getStatus, 1));
    }
}
